package avicit.im.lantop.basic.archivetemplatemanager.action;

import avicit.im.lantop.basic.archivetemplatemanager.service.ArchivetemplatemanagerService;
import avicit.im.lantop.basic.archivetemplatemanager.service.api.ArchivetemplatemanagerServiceApi;
import avicit.platform6.api.session.SessionHelper;
import avicit.platform6.api.syslookup.SysLookupAPI;
import avicit.platform6.api.syslookup.impl.SysLookupAPImpl;
import avicit.platform6.core.context.ThreadContextHelper;
import avicit.platform6.core.spring.SpringFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>金航数码科技有限责任公司</p>
 * <p>作者：钟一华</p>
 * <p>邮箱：feifan1985@163.com</p>
 * <p>创建时间： 2014-9-29 上午10:56:22 </p>
 * <p>类说明：Archivetemplatemanager模块controller类</p>
 * <p>修改记录：张坚，2014.11.29； 修改内容：新增归档树节点名称唯一性校验 </p>
 */
@Controller
@SuppressWarnings({"deprecation"})
@RequestMapping("/archivetemplatemanageController")
public class ArchivetemplatemanagerController {

	@Resource
	private ArchivetemplatemanagerServiceApi archivetemplatemanagerService;

	// 获取通用代码
	SysLookupAPI upLoader = SpringFactory.getBean(SysLookupAPImpl.class);

	/**
	 * 新增项目和计划公用方法
	 * @author 赵小龙
	 * 创建日期：2019/4/23
	 * @param req
	 * @return
	 */
	@RequestMapping("/saveProjectAndPlan")
	@ResponseBody
	public Map<String,Object> saveProjectAndPlan(HttpServletRequest req) {
		Map<String,Object> params = new HashMap<>();
		// 获取需要新增保存的数据
		String orgId = ThreadContextHelper.getOrgId();
		String deptId = ThreadContextHelper.getDeptId();
		String sysId = req.getParameter("sysId");
		String planId = req.getParameter("planId");
		String commitTypeto = req.getParameter("commitTypeto");
		String projectName = req.getParameter("projectName");
		String planNameto = req.getParameter("planNameto");
		this.archivetemplatemanagerService.updateProjct(planId, planNameto);
		String Id = req.getParameter("Id");
		if (Id != null) {// 新增项目
			// 对前台返回的字符串进行拆分，获取数据批量插入
			String [] arg = Id.split("/");
			String [] argMsg;
			Integer count = 0 ;
			for (int i = 0; i < arg.length; i++) {
				argMsg=arg[i].split(",");
				String id = argMsg[0].replace("'", "");
				if(id.equals("undefined")||"".equals(id)){
					count = count+1;
					String projectNameNew = argMsg[1].replace("'", "");
					params = this.archivetemplatemanagerService.saveProject(orgId, deptId, sysId, projectNameNew, commitTypeto, planId,count);
				}else{
					String projectNameNew = argMsg[1].replace("'", "");
					params = this.archivetemplatemanagerService.updateProjct(id, projectNameNew);
				}
			}
			// 返回提示信息
			return params;
		} else {  //新增计划
			// 对前台返回的字符串进行拆分，获取数据批量插入
			Integer count = 0 ;
			String [] arg = projectName.split("/");
			String [] argMsg;
			for (int i = 0; i < arg.length; i++) {
				argMsg=arg[i].split(",");
				String id = argMsg[0].replace("'", "");
				if(id.equals("undefined")||"".equals(id)){
					count = count+1;
					String projectNameNew = argMsg[1].replace("'", "");
					params = this.archivetemplatemanagerService.saveProject(orgId, deptId, sysId, projectNameNew, commitTypeto, planId,count);
				}else{
					String projectNameNew = argMsg[1].replace("'", "");
					params = this.archivetemplatemanagerService.updateProjct(id, projectNameNew);
				}
			}
			// 返回提示信息
			return params;
		}
	}

	/**
	 * 查询计划底下所有项目数据
	 * 创建日期：2019/4/23
	 * @author 赵小龙
	 * @param req
	 * @return
	 */
	@RequestMapping("/selectProject")
	@ResponseBody
	public Map<String,Object> selectProject(HttpServletRequest req) {
		Map<String,Object> params = new HashMap<>();
		String planName = req.getParameter("planName"); // 获取计划ID
		// 根据计划ID查询当前节点下所有的项目数据
		List<Map<String, Object>> list = this.archivetemplatemanagerService.selectProject(planName);
		List<Map<String, Object>> listData = new ArrayList<>();
		int count = 1;
		for (Map<String, Object> map : list) {
			map.put("numCount", count);
			count++;
			listData.add(map);
		}
		// 返回项目数据信息
		params.put("rows", listData);
		params.put("total", listData.size());
		return params;
	}

	/**
	 * 更新编辑项目信息
	 * 创建日期：2019/4/22
	 * @author 赵小龙
	 * @param req
	 * @return
	 */
	@RequestMapping("/updateProjct")
	@ResponseBody
	public Map<String,Object> updateProjct(HttpServletRequest req) {
		String planNameto = req.getParameter("planNameto");    // 计划ID
		String projectName = req.getParameter("projectName");   // 更新的项目名称
		Map<String,Object> params = this.archivetemplatemanagerService.updateProjct(planNameto, projectName);
		return params;
	}

	/**
	 * 新增项目信息
	 * 创建日期：2019/04/19
	 * @author 赵小龙
	 * @return
	 */
	@RequestMapping("/saveProject")
	@ResponseBody
	public Map<String,Object> saveProject(HttpServletRequest req) {
		Map<String,Object> params = new HashMap<>();
		String orgId = ThreadContextHelper.getOrgId();
		String deptId = ThreadContextHelper.getDeptId();
		String sysId = req.getParameter("sysId");
		String projectName = req.getParameter("projectName");
		String type = req.getParameter("commitTypeto");
		String planNameto = req.getParameter("planNameto");
		// 如果计划名称为空或者为null 返回保存失败
		if ("".equals(projectName) && projectName == null) {
			return (Map<String, Object>) params.put("success", 0);
		}
		return archivetemplatemanagerService.saveProject(orgId, deptId, sysId, projectName, type, planNameto,0);
	}

	/**
	 * 新增归档模板计划信息
	 * 创建日期：2019/04/19
	 * @author 赵小龙
	 * @return
	 */
	@RequestMapping("/savePlan")
	@ResponseBody
	public Map<String,Object> savePlan(HttpServletRequest req) {
		Map<String,Object> params = new HashMap<>();
		String orgId = ThreadContextHelper.getOrgId();
		String deptId = ThreadContextHelper.getDeptId();
		String sysId = req.getParameter("sysId");
		String planName = req.getParameter("planName");
		String type = req.getParameter("commitType");
		// 如果计划名称返回保存失败
		if ("".equals(planName) && planName == null) {
			return (Map<String, Object>) params.put("success", 0);
		}
		return archivetemplatemanagerService.savePlan(orgId, deptId, sysId, planName, type, null,0);
	}

	/**
	 * 查询归档模板信息
	 * 创建日期：2019/04/20
	 * @author 赵小龙
	 * @return
	 */
	@RequestMapping("/selectPlan")
	@ResponseBody
	public Map<String,Object> selectPlan(HttpServletRequest req) {
		Map<String,Object> parms = new HashMap<>();
		String plan = req.getParameter("commitType");
		String project = req.getParameter("commitTypeto");
		String orgId = ThreadContextHelper.getOrgId();
		List<Map<String, Object>> lists = new ArrayList<>();
		List<Map<String, Object>> listData = new ArrayList<>();
		if (plan == null && project != null) { // 获取项目数据
			lists = archivetemplatemanagerService.selectPlan(project,orgId);
		} else if (project == null && plan != null) {// 获取计划数据
			lists = archivetemplatemanagerService.selectPlan(plan,orgId);
		}
		int count = 1;
		for (Map<String, Object> map : lists) {
			map.put("numCount", count);
			count++;
			listData.add(map);
		}
		parms.put("rows", listData);
		parms.put("total", listData.size());
		return parms;
	}

	/**
	 * 删除计划
	 * @author 赵小龙
	 * @return
	 */
	@RequestMapping("/deletePlan")
	@ResponseBody
	public Map<String,Object> deletePlan(HttpServletRequest req) {
		Map<String,Object> params = new HashMap<>();
		String planName = req.getParameter("planName");
		// 计划名称为空保存失败
		if (planName == null && "".equals(planName)) {
			params.put("success", "0");
			return params;
		}
		// 拆分字符串进行批量删除
		String [] planId = planName.split(",");
		for (String value : planId) {
			String idVal = value.replace("'", "");
			archivetemplatemanagerService.deletePlan(idVal);
		}
		params.put("success", "1");
		return params;
	}

	@RequestMapping("/selectProjectList")
	@ResponseBody
	public Map<String,Object> selectProjectList(String id,Integer page,Integer rows){
		Map<String,Object> resultMap = archivetemplatemanagerService.selectProjectList(id,page,rows);
		return resultMap;
	}
}
